package com.aspire.moss.portal.desktop;

import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;

import com.aspire.moss.portal.R;
import com.aspire.moss.portal.desktop.AppView.OnDeleteClickListener;

/**
 * 底部的应用程序台
 * 
 * @author Administrator
 * 
 */
public class Dock extends ViewGroup implements Observer {

	private DataAdapter adapter;

	/**
	 * 最多支持4个应用
	 * 
	 * */
	 private static int MAX_CHILD = 4;
	 

	private int mWidth, mHeight;// 控件的宽度,高度
	
	private int cellWidth;// 每个子控件应该占的高度与宽度,但是控件要排在这个宽度中间的

	public Dock(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mWidth = MeasureSpec.getSize(widthMeasureSpec);
		mHeight = MeasureSpec.getSize(heightMeasureSpec);
		View child = null;
		for (int i = 0; i < getChildCount(); i++) {
			child = getChildAt(i);
			child.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		}
		cellWidth = (mWidth - getPaddingLeft() - getPaddingRight()) / MAX_CHILD;
		setMeasuredDimension(resolveSize(mWidth, widthMeasureSpec), resolveSize(mHeight, heightMeasureSpec));
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		View child = null;
		int left = getPaddingLeft() ;
		final int sum = (getChildCount() * cellWidth);
		final int center = mWidth - left - getPaddingRight();
		final int leftOffset = (center - sum) / 2;
		left += leftOffset;
		for(int i = 0 ; i < getChildCount() ; i++){
			child = getChildAt(i);
			if(i!=0){
				left += cellWidth ;
			}
			child.layout(left , 0, left + cellWidth, mWidth);
		}
	}

	public void moveChild(int index, int moveSize) {

	}

	@Override
	public void update(Observable observable, Object data) {
		removeAllViews();
		View view = null;
		for (int i = 0; i < adapter.getCount(); i++) {
			view = adapter.getView(i);
			this.addView(view);
		}
	}

	public DataAdapter getAdapter() {
		return adapter;
	}

	public void setAdapter(DataAdapter adapter) {
		this.adapter = adapter;
		if (null != this.adapter) {
			this.adapter.addObserver(this);
		}
	}

}

/**
 * Dock的数据适配器,配置好数据,调用notifyDataSetChanged将数据显示到控件,观察者模式
 * @author Administrator
 *
 */
final class DataAdapter extends Observable {

	private LinkedList<AppInfo> bottomApps;

	private Context mContext;

	private OnClickListener onClickListener;

	private OnDeleteClickListener onDeleteClickListener;

	private OnLongClickListener onLongClickListener;

	public DataAdapter(Context mContext, LinkedList<AppInfo> bottomApps) {
		super();
		this.mContext = mContext;
		this.bottomApps = bottomApps;
	}

	public void notifyDataSetChanged() {
		setChanged();
		notifyObservers();
	}

	public int getCount() {
		return bottomApps.size();
	}

	public AppInfo getItem(int position) {
		return bottomApps.get(position);
	}

	public AppView getView(int position) {
		final AppInfo info = bottomApps.get(position);
		final AppView view = (AppView) Utils.getLayoutInflater(mContext)
				.inflate(R.layout.app, null);
		view.appInfo = info;
//		final TextView textView = ((TextView) view.findViewById(R.id.title));
		if (null != info.iconBitmap) {
//			((ImageView) view.findViewById(R.id.icon))
//					.setImageBitmap(info.iconBitmap);
			view.setIcon(info.iconBitmap , false);
		}
		view.setText(info.title);
		if (null != onClickListener)
			view.setOnClickListener(onClickListener);
		if (null != onLongClickListener)
			view.setOnLongClickListener(onLongClickListener);
		if (null != onDeleteClickListener) {
			view.setOnDeleteClickListener(onDeleteClickListener);
		}
		view.postInvalidate();
		return view;
	}

	public OnClickListener getOnClickListener() {
		return onClickListener;
	}

	public void setOnClickListener(OnClickListener onClickListener) {
		this.onClickListener = onClickListener;
	}

	public OnDeleteClickListener getOnDeleteClickListener() {
		return onDeleteClickListener;
	}

	public void setOnDeleteClickListener(
			OnDeleteClickListener onDeleteClickListener) {
		this.onDeleteClickListener = onDeleteClickListener;
	}

	public OnLongClickListener getOnLongClickListener() {
		return onLongClickListener;
	}

	public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
		this.onLongClickListener = onLongClickListener;
	}

}
